package com.yoo.module_base.view


import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import com.yoo.module_base.utils.ScreenUtils
import com.youth.banner.indicator.BaseIndicator

class CustomVerticalIndicator @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : BaseIndicator(context, attrs, defStyleAttr) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = 0xFFD2D2D2.toInt()  // 默认颜色
    }
    private val selectedPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = 0xFFFF3B99.toInt()  // 选中颜色
    }
    private val rectF = RectF()
    private var count = 0
    private var currentIndex = 0
    private val INDICATOR_WIDTH = ScreenUtils.dip2px(context, 4f).toFloat()  // 指示器宽度
    private val SELECTED_HEIGHT = ScreenUtils.dip2px(context, 18f).toFloat()  // 选中项高度
    private val RADIUS = ScreenUtils.dip2px(context, 2f).toFloat()  // 圆角半径

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val totalHeight = count * SELECTED_HEIGHT
        val centerX = width / 2f
        val left = centerX - INDICATOR_WIDTH / 2
        val right = centerX + INDICATOR_WIDTH / 2

        // Draw the background indicator container
        rectF.set(left, 0f, right, totalHeight)
        canvas.drawRoundRect(rectF, RADIUS, RADIUS, paint)

        // Draw the selected item
        val top = currentIndex * SELECTED_HEIGHT
        val bottom = top + SELECTED_HEIGHT
        rectF.set(left, top, right, bottom)
        canvas.drawRoundRect(rectF, RADIUS, RADIUS, selectedPaint)
    }

    override fun getIndicatorView(): View {
        if (config.isAttachToBanner) {
            val layoutParams = FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT
            )
            layoutParams.gravity = Gravity.RIGHT or Gravity.TOP
            layoutParams.leftMargin = config.getMargins().leftMargin
            layoutParams.rightMargin = config.getMargins().rightMargin
            layoutParams.topMargin = ScreenUtils.dip2px(context, 194f)
            layoutParams.bottomMargin = config.getMargins().bottomMargin
            this.layoutParams = layoutParams
        }

        return this
    }

    fun setIndicatorCount(count: Int) {
        this.count = count
        requestLayout() // Ensure the view is remeasured and redrawn
    }

    fun setCurrentIndex(currentIndex: Int) {
        this.currentIndex = currentIndex
        invalidate()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val desiredWidth = INDICATOR_WIDTH.toInt()
        val desiredHeight = (count * SELECTED_HEIGHT).toInt()
        val width = resolveSize(desiredWidth, widthMeasureSpec)
        val height = resolveSize(desiredHeight, heightMeasureSpec)
        setMeasuredDimension(width, height)
    }
}


